<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款单号" prop="refundmentNo">
        <el-input
          v-model="queryParams.refundmentNo"
          placeholder="请输入退款单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="退款状态" prop="step">
        <el-select
          v-model="queryParams.step"
          placeholder="退款状态"
          disabled
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="dict in order_refundment_step_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间：" prop="rangeTime" label-width="85">
        <el-date-picker
          v-model="queryParams.rangeTime"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date(2023, 2, 1, 0, 0,0,0), new Date(2023, 2, 5, 23, 59,59,0)]"
          align="right"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="客户名称" prop="fullName">
        <el-input
          v-model="queryParams.fullName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户简称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入客户简称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yanjing:refundment:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yanjing:refundment:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yanjing:refundment:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yanjing:refundment:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="refundmentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="60"></el-table-column>
      <el-table-column label="客户简称" align="center" prop="companyName" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="退款单号" align="center" prop="refundmentNo" />
      <el-table-column label="退款类型" align="center" prop="refundmentType">
        <template #default="scope">
          <dict-tag
            :options="order_refundment_type"
            :value="scope.row.refundmentType"
          />
        </template>
      </el-table-column>
      <el-table-column label="退款状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag
            :options="order_refundment_step_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="退款金额" align="center" prop="oughtAmount">
        <template #default="scope">
          {{ (Math.round(scope.row.oughtAmount) / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="已退金额" align="center" prop="actualAmount">
        <template #default="scope">
          {{ (Math.round(scope.row.actualAmount) / 100).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="发起者" align="center" prop="userName" />
      <el-table-column
        label="发起时间"
        align="center"
        prop="createTime"
        width="180"
      />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="View"
            @click="handleView(scope.row, scope.index)"
            >详细</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yanjing:refundment:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yanjing:refundment:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page.sync="queryParams.pageNum"
      v-model:limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="客户信息" prop="refundmentNo">
          <el-input
            type="textarea"
            v-model="companyAllInfo"
            placeholder="请输入客户信息"
          />
        </el-form-item>
        <el-row>
          <el-col :span="10">
            <el-form-item label="订单编号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="退款单号" prop="refundmentNo">
              <el-input
                v-model="form.refundmentNo"
                placeholder="请输入退款单号"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="退款类型" prop="refundmentType">
              <el-select
                v-model="form.refundmentType"
                placeholder="请选择退款类型"
                clearable
              >
                <el-option
                  v-for="item in order_refundment_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="退款状态" prop="status">
              <el-select
                v-model="form.status"
                placeholder="请选择退款状态"
                clearable
              >
                <el-option
                  v-for="item in order_refundment_step_status"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="退款金额" prop="oughtAmount">
              <el-input
                v-model="form.oughtAmount"
                :formatter="formatPrice"
                placeholder="请输入退款金额"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="实退金额" prop="actualAmount">
              <el-input
                v-model="form.actualAmount"
                :formatter="formatPrice"
                placeholder="请输入实退金额"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="发起者" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入发起者" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="发起时间" prop="createTime">
              <el-input
                v-model="form.createTime"
                placeholder="请输入发起时间"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="退款原因" prop="memo">
          <el-input v-model="form.memo" placeholder="请输入退款原因" />
        </el-form-item>

        <el-form-item label="审核说明" prop="approveMome">
          <el-input v-model="form.approveMemo" placeholder="请输入审核说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button v-if="isDetail" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryPageByParam,
  getRefundment,
  delRefundment,
  addRefundment,
  updateRefundment,
} from "@/api/order/refundment";
import { formatDate } from "@/utils/date";
import { getCurrentInstance } from "vue";
export default {
  setup() {
    // 获取当前组件实例
    const instance = getCurrentInstance();
    const { order_refundment_type } = instance.proxy.useDict(
      "order_refundment_type"
    );
    const { order_refundment_step_status } = instance.proxy.useDict(
      "order_refundment_step_status"
    );

    return {
      order_refundment_type,
      order_refundment_step_status,
    };
  },
  name: "Refundment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      refundmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        step: "REFUNDED",
        pageNum: 1,
        pageSize: 10,
        refundmentNo: null,
        object: null,
        companyId: null,
        userId: null,
        orderId: null,
        orderNo: null,
        serveId: null,
        serveNo: null,
        refundmentType: null,
        gatewayType: null,
        type: null,
        oughtAmount: null,
        actualAmount: null,
        status: null,
        isauto: null,
        memo: null,
        allowTime: null,
        refundTime: null,
        complateTime: null,
        notified: null,
        notifyTime: null,
        createEmployeeId: null,
        createTime: null,
        approveEmployeeId: null,
        approveTime: null,
        approveMemo: null,
        startTime: null,
        endTime: null,
        rangeTime: ["", ""],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
    watch: {
    "queryParams.rangeTime"(newVal){
        if(newVal == null){
          this.queryParams.rangeTime =['',''];
        }
      }
  },
  created() {
    this.queryParams.step = "REFUNDED";
    this.getList();
  },
  methods: {
    formatPrice(value) {
      console.log("value:" + value);
      return (Math.round(value) / 100).toFixed(2) + "元";
    },
    handleView(row) {
      
      this.open = true;
      this.isDetail = false;
      this.title = "详情";
      this.form = { ...row };
      this.companyAllInfo =
        "客户编号【" +
        this.form.companyCode +
        "】" +
        "客户简称【" +
        this.form.companyName +
        "】" +
        "客户全称【" +
        this.form.fullName +
        "】";
      // this.form.approveMemo =
      //   this.form.approveMemo == null ? "审核" : this.form.approveMemo;
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.startTime =
        this.queryParams.rangeTime[0] === ""
          ? this.queryParams.rangeTime[0]
          : formatDate(this.queryParams.rangeTime[0], "yyyy-MM-dd hh:mm:ss");
      this.queryParams.endTime =
        this.queryParams.rangeTime[1] === ""
          ? this.queryParams.rangeTime[1]
          : formatDate(this.queryParams.rangeTime[1], "yyyy-MM-dd hh:mm:ss");
      queryPageByParam(this.queryParams).then((response) => {
        this.refundmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        refundmentId: null,
        refundmentNo: null,
        object: null,
        companyId: null,
        userId: null,
        orderId: null,
        orderNo: null,
        serveId: null,
        serveNo: null,
        refundmentType: null,
        gatewayType: null,
        type: null,
        oughtAmount: null,
        actualAmount: null,
        status: null,
        isauto: null,
        memo: null,
        allowTime: null,
        refundTime: null,
        complateTime: null,
        notified: null,
        notifyTime: null,
        createEmployeeId: null,
        createTime: null,
        approveEmployeeId: null,
        approveTime: null,
        approveMemo: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.refundmentId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const refundmentId = row.refundmentId || this.ids;
      getRefundment(refundmentId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.refundmentId != null) {
            updateRefundment(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRefundment(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const refundmentIds = row.refundmentId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除编号为"' +
            refundmentIds +
            '"的数据项？'
        )
        .then(function () {
          return delRefundment(refundmentIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "yanjing/refundment/export",
        {
          ...this.queryParams,
        },
        `refundment_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
